package com.unina.is2.astafantaunina.activity;

import com.unina.is2.astafantaunina.R;
import com.unina.is2.astafantaunina.chat.XMPP;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MenuActivity extends ActionBarActivity {

	private final static String MY_PREFERENCES = "UtenteAstaFantaunina";
	Context context;
	
	private String timer, crediti = new String();
	
	@Override
	public void onResume(){
		super.onStart();
		context = this;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		
		Spinner spinner_timer = (Spinner) findViewById(R.id.spinnerTimer);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
		        R.array.spinner_timer, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner_timer.setAdapter(adapter);
		
		spinner_timer.setOnItemSelectedListener(new OnItemSelectedListener() {
        	public void onItemSelected(AdapterView<?> adapter, View view,int pos, long id) {
        		timer = (String)adapter.getItemAtPosition(pos);
        		
        	}
        	public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
		Spinner spinner_crediti = (Spinner) findViewById(R.id.spinnerCrediti);
		// Create an ArrayAdapter using the string array and a default spinner layout
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
		        R.array.spinner_crediti, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner_crediti.setAdapter(adapter2);
		
		spinner_crediti.setOnItemSelectedListener(new OnItemSelectedListener() {
        	public void onItemSelected(AdapterView<?> adapter, View view,int pos, long id) {
        		crediti = (String)adapter.getItemAtPosition(pos);
        		
        	}
        	public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
		Button salva = (Button) findViewById(R.id.buttonSalva);
		
		salva.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SharedPreferences sharedPref =getSharedPreferences(MY_PREFERENCES,Context.MODE_PRIVATE);

				XMPP xmpp = XMPP.getSingletonXMPP(sharedPref.getString("username", ""), sharedPref.getString("password", ""));
				xmpp.sendXMPPmsg("7-"+(Integer.valueOf(timer)+1)+"000-"+crediti, sharedPref.getString("lega_scelta", "nessuna lega")+"@fantaunina");
				
				int indice = sharedPref.getInt("indice_lega", -1);
				
				String crediti_shared = sharedPref.getString("crediti", "");
				
				String[] split = crediti_shared.split("-");
				
				split[indice] = crediti;
				
				crediti_shared = "";
				
				for (int i=0;i<split.length;i++){
					
					if (i==split.length-1) crediti_shared+= split[i];
					else crediti_shared+= split[i]+"-";
					
				}
				
				
				SharedPreferences.Editor editor = sharedPref.edit();
				editor.putString("crediti", crediti_shared);
				
				editor.commit();
				
				System.out.println("MSG INVIATO "+"7-"+timer+"-"+crediti);
				
				Intent intent = new Intent(context, LoggedActivity.class);    
		        startActivity(intent);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


}
